package com.sinodata.bsm.cicp.finder.solaris;

/**
 * 文件系统发现程序
 * 程序返回：文件系统的所属逻辑卷设备，挂载点
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class FS extends SolarisFinder {

    private static final Logger logger = Logger.getLogger(FS.class);
    //private static final String SHELL = "df -k";//df -F ufs -o i |awk '{ if(NF==5) printf("%s\t\n", $0); else print $0;}'
    private static final String SHELL = "df -F ufs -o i |awk '{ if(NF==5) printf(\"%s\t\n\", $0); else print $0;}'";

    public static void main(String[] args) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] fsRes = null;
        BufferedReader reader = null;
        String strLine = null;
        String regex = "(.*)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+%)\\s+(.*)\\s*";
        try {
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            Pattern pattern = Pattern.compile(regex);
            while ((strLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    fsRes = new String[5];
                    list.add(fsRes);
                    fsRes[0] = matcher.group(6);
                    fsRes[1] = matcher.group(6);
                    fsRes[2] = getHostName(ip) + " FS " + matcher.group(6);
                    fsRes[3] = matcher.group(1); //所属逻辑卷设备
                    fsRes[4] = matcher.group(6); //挂载点
                }
            }
            return list;
        } catch (IOException e) {
            logger.error("failed to find Solaris FS", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }
}
